Tubes.Networking = Tubes.Networking or {}
local Networking = Tubes.Networking
Networking.Readers = {}
Networking.Senders = {}
Networking.StringPool = Tubes.StringPool (65536)

function Networking.RegisterReader (name, reader)
	Networking.Readers [name] = reader
	if CLIENT then
		usermessage.Hook ("T" .. name, function (umsg)
			ErrorNoHalt ("Received umsg " .. name .. "\n")
			Networking.Readers [name] (Networking.NetMessageReader (umsg))
		end)
	end
	
	Networking.StringPool:AddString (name)
end

function Networking.RegisterSender (name, reader)
	Networking.Senders [name] = reader
	
	Networking.StringPool:AddString (name)
end

function Networking.Send (name, recipients, ...)
	local netMessage = Networking.NetMessage (name, recipients)
	ErrorNoHalt ("Sending umsg " .. name .. "\n")
	Networking.Senders [name] (netMessage, ...)
	netMessage:End ()
end

include ("c2s_Shared.lua")
if CLIENT then
	include ("c2s_NetMessage.lua")
	include ("s2c_NetMessageDatastreamReader.lua")
	include ("s2c_NetMessageReader.lua")
else
	include ("s2c_NetMessage.lua")
	include ("c2s_NetMessageReader.lua")
end